package com.zhongen.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongen.admin.api.entity.SysUser;
import com.zhongen.oa.entity.ProjectTeamEntity;
import com.zhongen.oa.entity.ProjectTeamUserRelationshipEntity;
import com.zhongen.oa.mapper.ProjectTeamMapper;
import com.zhongen.oa.service.ProjectTeamService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目团队表
 *
 * @author Code Generator
 * @date 2024-01-11 13:59:55
 */
@Service
@AllArgsConstructor
public class ProjectTeamServiceImpl extends ServiceImpl<ProjectTeamMapper, ProjectTeamEntity>
		implements ProjectTeamService {

	private final ProjectTeamMapper projectTeamMapper;

	@Override
	public boolean save(ProjectTeamEntity entity) {
		if (super.save(entity)) {
			if (ObjectUtils.isNotEmpty(entity.getUserList())) {
				// 项目成员关联
				List<Long> userList = entity.getUserList()
					.stream()
					.map(SysUser::getUserId)
					.collect(Collectors.toList());
				projectTeamMapper.insertTeamMembers(entity.getId(), userList);
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean updateById(ProjectTeamEntity entity) {
		if (super.updateById(entity)) {
			if (ObjectUtils.isNotEmpty(entity.getUserList())) {
				List<Long> userList = entity.getUserList()
					.stream()
					.map(SysUser::getUserId)
					.collect(Collectors.toList());
				List<Long> users = new ArrayList<>(userList);
				// 去重
				List<ProjectTeamUserRelationshipEntity> projectTeamUserRelationshipEntityList = projectTeamMapper
					.selectTeamUser(entity.getId());
				List<Long> teamUserList = projectTeamUserRelationshipEntityList.stream()
					.filter(ObjectUtils::isNotEmpty)
					.map(ProjectTeamUserRelationshipEntity::getUserId)
					.collect(Collectors.toList());
				// 新增
				userList.removeAll(teamUserList);
				// 删除
				teamUserList.removeAll(users);
				// 项目成员关联
				if (ObjectUtils.isNotEmpty(userList)) {
					projectTeamMapper.insertTeamMembers(entity.getId(), userList);
				}
				if (ObjectUtils.isNotEmpty(teamUserList)) {
					projectTeamMapper.deleteTeamUsers(entity.getId(), teamUserList);
				}
			}
			else {
				projectTeamMapper.deleteTeamUsers(entity.getId(), null);
			}
			return true;
		}
		return false;
	}

	@Override
	public IPage<ProjectTeamEntity> getProjectTeamPage(Page page, ProjectTeamEntity projectTeamEntity) {
		return projectTeamMapper.queryTeamInfoList(page, projectTeamEntity);
	}

	@Override
	public boolean teamBreakById(Long id) {
		UpdateWrapper<ProjectTeamEntity> warper = new UpdateWrapper<ProjectTeamEntity>();
		warper.set("team_status", "2").eq("id", id).eq("del_flag", "0");
		projectTeamMapper.update(null, warper);
		return true;
	}

	@Override
	public boolean addTeamMembers(ProjectTeamUserRelationshipEntity relationshipEntity) {
		try {
			projectTeamMapper.addTeamMembers(relationshipEntity);
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
